'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class FlapRequestSignInCookie
    Inherits Flap

    Public Sub New()
        MyBase.New(FlapChannel.NewConnectionNegotiation)
    End Sub

    Private _ScreenName As New TlvScreenName
    Public ReadOnly Property ScreenName() As TlvScreenName
        Get
            Return _ScreenName
        End Get
    End Property

    Private _Password As New TlvPassword
    Public ReadOnly Property Password() As TlvPassword
        Get
            Return _Password
        End Get
    End Property

    Private _ClientIdString As New TlvClientIdString
    Public ReadOnly Property ClientIdString() As TlvClientIdString
        Get
            Return _ClientIdString
        End Get
    End Property

    Private _ClientId As New TlvClientId
    Public ReadOnly Property ClientId() As TlvClientId
        Get
            Return _ClientId
        End Get
    End Property

    Private _ClientMajorVersion As New TlvClientMajorVersion
    Public ReadOnly Property ClientMajorVersion() As TlvClientMajorVersion
        Get
            Return _ClientMajorVersion
        End Get
    End Property

    Private _ClientMinorVersion As New TlvClientMinorVersion
    Public ReadOnly Property ClientMinorVersion() As TlvClientMinorVersion
        Get
            Return _ClientMinorVersion
        End Get
    End Property

    Private _ClientLesserVersion As New TlvClientLesserVersion
    Public ReadOnly Property ClientLesserVersion() As TlvClientLesserVersion
        Get
            Return _ClientLesserVersion
        End Get
    End Property

    Private _ClientBuildNumber As New TlvClientBuildNumber
    Public ReadOnly Property ClientBuildNumber() As TlvClientBuildNumber
        Get
            Return _ClientBuildNumber
        End Get
    End Property

    Private _ClientDistributionNumber As New TlvClientDistributionNumber
    Public ReadOnly Property ClientDistributionNumber() As TlvClientDistributionNumber
        Get
            Return _ClientDistributionNumber
        End Get
    End Property

    Private _ClientLanguage As New TlvClientLanguage
    Public ReadOnly Property ClientLanguage() As TlvClientLanguage
        Get
            Return _ClientLanguage
        End Get
    End Property

    Private _ClientCountry As New TlvClientCountry
    Public ReadOnly Property ClientCountry() As TlvClientCountry
        Get
            Return _ClientCountry
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        Throw New NotImplementedException
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 4 + _ScreenName.CalculateTotalSize + _Password.CalculateTotalSize + _ClientIdString.CalculateTotalSize + _ClientId.CalculateTotalSize + _ClientMajorVersion.CalculateTotalSize + _ClientMinorVersion.CalculateTotalSize + _ClientLesserVersion.CalculateTotalSize + _ClientBuildNumber.CalculateTotalSize + _ClientDistributionNumber.CalculateTotalSize + _ClientLanguage.CalculateTotalSize + _ClientCountry.CalculateTotalSize
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUInt(1)))
        data.AddRange(_ScreenName.Serialize)
        data.AddRange(_Password.Serialize)
        data.AddRange(_ClientIdString.Serialize)
        data.AddRange(_ClientId.Serialize)
        data.AddRange(_ClientMajorVersion.Serialize)
        data.AddRange(_ClientMinorVersion.Serialize)
        data.AddRange(_ClientLesserVersion.Serialize)
        data.AddRange(_ClientBuildNumber.Serialize)
        data.AddRange(_ClientDistributionNumber.Serialize)
        data.AddRange(_ClientLanguage.Serialize)
        data.AddRange(_ClientCountry.Serialize)

        Return data
    End Function
End Class

Public Class TlvScreenName
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Private _Uin As String
    Public Property Uin() As String
        Get
            Return _Uin
        End Get
        Set(ByVal value As String)
            _Uin = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _Uin.Length
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_Uin))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _Uin = ByteConverter.ToString(data.GetRange(index, DataSize))
    End Sub
End Class

Public Class TlvClientIdString
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H3)
    End Sub

    Private _ClientIdString As String
    Public Property ClientIdString() As String
        Get
            Return _ClientIdString
        End Get
        Set(ByVal value As String)
            _ClientIdString = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _ClientIdString.Length
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_ClientIdString))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientIdString = ByteConverter.ToString(data.GetRange(index, DataSize))
    End Sub
End Class

Public Class TlvClientLanguage
    Inherits Tlv

    Public Sub New()
        MyBase.New(&HF)
    End Sub

    Private _ClientLanguage As String
    Public Property ClientLanguage() As String
        Get
            Return _ClientLanguage
        End Get
        Set(ByVal value As String)
            _ClientLanguage = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _ClientLanguage.Length
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_ClientLanguage))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientLanguage = ByteConverter.ToString(data.GetRange(index, DataSize))
    End Sub
End Class

Public Class TlvClientCountry
    Inherits Tlv

    Public Sub New()
        MyBase.New(&HE)
    End Sub

    Private _ClientCountry As String
    Public Property ClientCountry() As String
        Get
            Return _ClientCountry
        End Get
        Set(ByVal value As String)
            _ClientCountry = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _ClientCountry.Length
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_ClientCountry))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientCountry = ByteConverter.ToString(data.GetRange(index, DataSize))
    End Sub
End Class

Public Class TlvClientId
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H16)
    End Sub

    Private _ClientId As Integer
    Public Property ClientId() As Integer
        Get
            Return _ClientId
        End Get
        Set(ByVal value As Integer)
            _ClientId = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientId)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientId = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub
End Class

Public Class TlvClientMajorVersion
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H17)
    End Sub

    Private _ClientMajorVersion As Integer
    Public Property ClientMajorVersion() As Integer
        Get
            Return _ClientMajorVersion
        End Get
        Set(ByVal value As Integer)
            _ClientMajorVersion = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientMajorVersion)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientMajorVersion = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub
End Class

Public Class TlvClientMinorVersion
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H18)
    End Sub

    Private _ClientMinorVersion As Integer
    Public Property ClientMinorVersion() As Integer
        Get
            Return _ClientMinorVersion
        End Get
        Set(ByVal value As Integer)
            _ClientMinorVersion = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientMinorVersion)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientMinorVersion = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub
End Class

Public Class TlvClientLesserVersion
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H19)
    End Sub

    Private _ClientLesserVersion As Integer
    Public Property ClientLesserVersion() As Integer
        Get
            Return _ClientLesserVersion
        End Get
        Set(ByVal value As Integer)
            _ClientLesserVersion = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientLesserVersion)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientLesserVersion = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub
End Class

Public Class TlvClientBuildNumber
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1A)
    End Sub

    Private _ClientBuildNumber As Integer
    Public Property ClientBuildNumber() As Integer
        Get
            Return _ClientBuildNumber
        End Get
        Set(ByVal value As Integer)
            _ClientBuildNumber = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientBuildNumber)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientBuildNumber = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub
End Class

Public Class TlvClientDistributionNumber
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H14)
    End Sub

    Private _ClientDistributionNumber As Long
    Public Property ClientDistributionNumber() As Long
        Get
            Return _ClientDistributionNumber
        End Get
        Set(ByVal value As Long)
            _ClientDistributionNumber = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return 4
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUInt(_ClientDistributionNumber)))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientDistributionNumber = ByteConverter.ToUInt32(data.GetRange(index, 4))
    End Sub
End Class

Public Class TlvPassword
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H2)
    End Sub

    Private _Password As String
    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _Password.Length
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetXorHashFromPassword(_Password.ToCharArray))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        Throw New NotImplementedException
    End Sub
End Class
